﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell;
using System.Diagnostics;

namespace TorstenMandelkow.SharePointSoftwareFactory_HiveViewer
{
    /// <summary>
    /// This class implements the tool window exposed by this package and hosts a user control.
    ///
    /// In Visual Studio tool windows are composed of a frame (implemented by the shell) and a pane, 
    /// usually implemented by the package implementer.
    ///
    /// This class derives from the ToolWindowPane class provided from the MPF in order to use its 
    /// implementation of the IVsUIElementPane interface.
    /// </summary>
    [Guid("21011018-c4e4-4739-92bb-10ef4bf5d259")]
    public class MyToolWindow : ToolWindowPane
    {
        EnvDTE.DTE dte;

        /// <summary>
        /// Standard constructor for the tool window.
        /// </summary>
        public MyToolWindow() :
            base(null)
        {
            // Set the window title reading it from the resources.
            this.Caption = Resources.ToolWindowTitle;
            // Set the image that will appear on the tab of the window frame
            // when docked with an other window
            // The resource ID correspond to the one defined in the resx file
            // while the Index is the offset in the bitmap strip. Each image in
            // the strip being 16x16.
            this.BitmapResourceID = 301;
            this.BitmapIndex = 1;

            // This is the user control hosted by the tool window; Note that, even if this class implements IDisposable,
            // we are not calling Dispose on this object. This is because ToolWindowPane calls Dispose on 
            // the object returned by the Content property.
            base.Content = new MyControl();
        }

        void UpdateContents(string content)
        {
            try
            {
                if (base.Content is MyControl)
                {
                    MyControl c = base.Content as MyControl;
                    c.Update(content);
                }
            }
            catch { }
        }

        void SolutionItemsEvents_ItemRenamed(EnvDTE.ProjectItem ProjectItem, string OldName)
        {
            UpdateContents("SolutionItemsEvents_ItemRenamed");
        }

        void SolutionItemsEvents_ItemRemoved(EnvDTE.ProjectItem ProjectItem)
        {
            UpdateContents("SolutionItemsEvents_ItemRemoved");
        }

        void SolutionItemsEvents_ItemAdded(EnvDTE.ProjectItem ProjectItem)
        {
            UpdateContents("SolutionItemsEvents_ItemAdded");
        }

        internal void RegisterEvents(EnvDTE80.DTE2 dte2)
        {
            if (base.Content is MyControl)
            {
                MyControl c = base.Content as MyControl;
                c.SetDTE(dte2);
            }

            try
            {
                dte2.Events.MiscFilesEvents.ItemAdded += new EnvDTE._dispProjectItemsEvents_ItemAddedEventHandler(MiscFilesEvents_ItemAdded);
                dte2.Events.SolutionItemsEvents.ItemAdded += new EnvDTE._dispProjectItemsEvents_ItemAddedEventHandler(SolutionItemsEvents_ItemAdded);
                dte2.Events.SolutionItemsEvents.ItemRemoved += new EnvDTE._dispProjectItemsEvents_ItemRemovedEventHandler(SolutionItemsEvents_ItemRemoved);
                dte2.Events.SolutionItemsEvents.ItemRenamed += new EnvDTE._dispProjectItemsEvents_ItemRenamedEventHandler(SolutionItemsEvents_ItemRenamed);

                dte2.Events.SolutionEvents.Opened += new EnvDTE._dispSolutionEvents_OpenedEventHandler(SolutionEvents_Opened);

                EnvDTE80.Events2 events2 = dte2.Events as EnvDTE80.Events2;
                events2.ProjectItemsEvents.ItemAdded += new EnvDTE._dispProjectItemsEvents_ItemAddedEventHandler(ProjectItemsEvents_ItemAdded);
                events2.ProjectItemsEvents.ItemRemoved += new EnvDTE._dispProjectItemsEvents_ItemRemovedEventHandler(ProjectItemsEvents_ItemRemoved);
                events2.ProjectItemsEvents.ItemRenamed += new EnvDTE._dispProjectItemsEvents_ItemRenamedEventHandler(ProjectItemsEvents_ItemRenamed);

                events2.ProjectsEvents.ItemAdded += new EnvDTE._dispProjectsEvents_ItemAddedEventHandler(ProjectsEvents_ItemAdded);
                events2.ProjectsEvents.ItemRemoved += new EnvDTE._dispProjectsEvents_ItemRemovedEventHandler(ProjectsEvents_ItemRemoved);
                events2.ProjectsEvents.ItemRenamed += new EnvDTE._dispProjectsEvents_ItemRenamedEventHandler(ProjectsEvents_ItemRenamed);
            }
            catch (Exception ex)
            {
                //Trace.TraceInformation(ex.ToString());
                MessageBox.Show(ex.Message);
            }
        }

        void ProjectsEvents_ItemRenamed(EnvDTE.Project Project, string OldName)
        {
            UpdateContents("ProjectsEvents_ItemRenamed");
        }

        void ProjectsEvents_ItemRemoved(EnvDTE.Project Project)
        {

            UpdateContents("ProjectsEvents_ItemRemoved");
        }

        void ProjectItemsEvents_ItemRenamed(EnvDTE.ProjectItem ProjectItem, string OldName)
        {
            UpdateContents("ProjectItemsEvents_ItemRenamed");
        }

        void ProjectItemsEvents_ItemRemoved(EnvDTE.ProjectItem ProjectItem)
        {
            UpdateContents("ProjectItemsEvents_ItemRemoved");
        }

        void ProjectsEvents_ItemAdded(EnvDTE.Project Project)
        {
            UpdateContents("ProjectsEvents_ItemAdded");
        }

        void ProjectItemsEvents_ItemAdded(EnvDTE.ProjectItem ProjectItem)
        {
            UpdateContents("ProjectItemsEvents_ItemAdded");
        }

        void SolutionEvents_Opened()
        {
            UpdateContents("SolutionEvents_Opened");
        }

        void MiscFilesEvents_ItemAdded(EnvDTE.ProjectItem ProjectItem)
        {
            UpdateContents("MiscFilesEvents_ItemAdded");
        }
    }
}
